﻿using CCWin.SkinClass;
using HirelSeriesH.Entity;
using HirelSeriesH.Tools;
using MySql.Data.MySqlClient;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace HirelSeriesH
{
    public partial class ConfigDetailForm : UIForm
    {
        private string action;
        private string person;
        private string oldParaName = "";
        private DataRow data;
        private List<UICheckBox> cbs = new List<UICheckBox>();
        private List<UIDoubleUpDown> ups = new List<UIDoubleUpDown>();

        private List<UIDoubleUpDown> onList = new List<UIDoubleUpDown>();
        private List<UIDoubleUpDown> offList = new List<UIDoubleUpDown>();

        // 子窗口声明定义委托 refresh() ParaName
        public delegate void RefreshDelegate();
        public static event RefreshDelegate refreshData;
        public ConfigDetailForm(string person, string action, DataRow dataRow)
        {
            InitializeComponent();
            this.action = action;
            this.person = person;
            this.data = dataRow;

            if (this.action == "create")
            {
                this.Text = "新建测试参数";
                this.btnSave.Text = "保    存";
            }
            if (this.action == "modify")
            {
                this.Text = "修改测试参数";
                this.btnSave.Text = "修    改";
            }
            if (this.action == "read")
            {
                this.Text = "查看测试参数";
                this.btnSave.Enabled = false;
                //this.BtnBack.Location = new Point(this.BtnBack.Location.X - 45, this.BtnBack.Location.Y);
            }
        }

        private void BtnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string ParaName = this.textBoxParaName.Text.Trim();
            string Remarks = this.textBoxRemarks.Text.Trim();
            if (ParaName == null || ParaName.Length == 0)
            {
                UIMessageDialog.ShowMessageDialog(this, "请输入测试方案名称", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }
            if (Remarks == null || Remarks.Length == 0)
            {
                UIMessageDialog.ShowMessageDialog(this, "请输入测试方案描述", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }

            string ProductModel = this.textBoxProductModel.Text.Trim();
            //检查文件名是否合法
            if (string.IsNullOrWhiteSpace(ProductModel))
            {
                UIMessageDialog.ShowMessageDialog(this, "产品型号不能为空，请输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                textBoxProductModel.Focus();
                return;
            }
            if (ProductModel.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
            {
                //含有非法字符 \ / : * ? " < > | 等
                UIMessageDialog.ShowMessageDialog(this, "产品型号含有非法字符，请重新输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                textBoxProductModel.Focus();
                return;
            }

            string BatchNumber = this.textBoxBatchNumber.Text.Trim();
            //检查文件名是否合法
            if (string.IsNullOrWhiteSpace(BatchNumber))
            {
                BatchNumber = "";
            }
            if (BatchNumber != "")
            {
                if (BatchNumber.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
                {
                    //含有非法字符 \ / : * ? " < > | 等
                    UIMessageDialog.ShowMessageDialog(this, "产品批号含有非法字符，请重新输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    textBoxBatchNumber.Focus();
                    return;
                }
            }
            decimal CheckInterval = this.NudCheckInterval.Value;
            if (CheckInterval == 0)
            {
                UIMessageDialog.ShowMessageDialog(this, "请输入采样间隔", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }

            if (PowerStepDataGrid.RowCount <= 0)
            {
                UIMessageDialog.ShowMessageDialog(this, "请配置电源组加电步骤！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                return;
            }

            //电源模式
            bool isStatic = uiRadioButton2.Checked;
            if (isStatic)
            {
                List<string> powerList = new List<string>();
                for (int rowIndex = 0; rowIndex < PowerStepDataGrid.RowCount; rowIndex++)
                {
                    DataGridViewRow row = PowerStepDataGrid.Rows[rowIndex];

                    if (((DataGridViewComboBoxCell)row.Cells[0]).Value != null)
                    {
                        string powerNo = ((DataGridViewComboBoxCell)row.Cells[0]).Value.ToString();
                        if (powerList.Contains(powerNo))
                        {

                            UIMessageDialog.ShowMessageDialog(this, "静态模式不能添加相同电源的步骤", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                            return;
                        }
                        else
                        {
                            powerList.Add(powerNo);
                        }
                    }
                }
            }


            string PowerStepConfig = "";
            for (int rowIndex = 0; rowIndex < PowerStepDataGrid.RowCount; rowIndex++)
            {
                DataGridViewRow row = PowerStepDataGrid.Rows[rowIndex];

                //电源编号
                if (((DataGridViewComboBoxCell)PowerStepDataGrid.Rows[rowIndex].Cells[0]).Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：电源未选择！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string PowerNo = ((DataGridViewComboBoxCell)PowerStepDataGrid.Rows[rowIndex].Cells[0]).Value.ToString();
                if (string.IsNullOrEmpty(PowerNo))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：电源未选择！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //起始电压
                if (PowerStepDataGrid.Rows[rowIndex].Cells[1].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：起始电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string StartVolt = PowerStepDataGrid.Rows[rowIndex].Cells[1].Value.ToString();
                if (string.IsNullOrEmpty(StartVolt))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：起始电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DStrartVolt = double.Parse(StartVolt);
                    if (DStrartVolt < 0)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：起始电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：起始电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //目标电压
                if (PowerStepDataGrid.Rows[rowIndex].Cells[2].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：目标电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string EndVolt = PowerStepDataGrid.Rows[rowIndex].Cells[2].Value.ToString();
                if (string.IsNullOrEmpty(EndVolt))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：目标电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DEndVolt = double.Parse(EndVolt);
                    if (DEndVolt < 0)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：目标电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：目标电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //步长电压
                if (PowerStepDataGrid.Rows[rowIndex].Cells[3].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：步长电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string StepVolt = PowerStepDataGrid.Rows[rowIndex].Cells[3].Value.ToString();
                if (string.IsNullOrEmpty(StepVolt))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：步长电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DStepVolt = double.Parse(StepVolt);
                    if (DStepVolt < 0)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：步长电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：步长电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //动作间隔
                if (PowerStepDataGrid.Rows[rowIndex].Cells[4].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：动作间隔未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string IntervalTime = PowerStepDataGrid.Rows[rowIndex].Cells[4].Value.ToString();
                if (string.IsNullOrEmpty(IntervalTime))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：动作间隔未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DIntervalTime = double.Parse(IntervalTime);
                    if (DIntervalTime < 5 || DIntervalTime > 3600)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：动作间隔输入错误，必须大于等于5且小于等于3600！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：动作间隔输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //延迟时间
                if (PowerStepDataGrid.Rows[rowIndex].Cells[5].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：延迟时间未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string DelayTime = PowerStepDataGrid.Rows[rowIndex].Cells[5].Value.ToString();
                if (string.IsNullOrEmpty(DelayTime))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：延迟时间未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DDelayTime = double.Parse(DelayTime);
                    if (DDelayTime <= 0 || DDelayTime > 3600)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：延迟时间输入错误，必须大于0且小于等于3600！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：延迟时间输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                PowerStepConfig = PowerStepConfig
                    + (rowIndex + 1) + ";"
                    + PowerNo + ";"
                    + StartVolt.ToString("0.00") + ";"
                    + EndVolt.ToString("0.00") + ";"
                    + StepVolt.ToString("0.00") + ";"
                    + IntervalTime.ToString("0.000") + ";"
                    + DelayTime.ToString("0.000");

                if ((rowIndex + 1) < PowerStepDataGrid.RowCount)
                {
                    PowerStepConfig = PowerStepConfig + "-";
                }
            }

            decimal HighTemp = this.HighTemperatureValue.Value;
            decimal LowTemp = this.LowTemperatureValue.Value;
            if (LowTemp >= HighTemp)
            {
                MessageBox.Show("高温温度必须大于低温温度", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            decimal HighTime = this.NumHighTime.Value;
            if (HighTime == 0)
            {
                MessageBox.Show("请输入高温时长", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            decimal LowTime = this.NumLowTime.Value;
            if (LowTime == 0)
            {
                MessageBox.Show("请输入低温时长", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string IsLineContrl;
            if (CBIsLineContrl.Checked)
            {
                IsLineContrl = "Y";
            }
            else
            {
                IsLineContrl = "N";
            }
            decimal XieLv = this.NumXieLv.Value;
            if (XieLv == 0)
            {
                MessageBox.Show("请输入斜率", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            decimal TestCycs = this.NumTestCycs.Value;
            if (TestCycs == 0)
            {
                MessageBox.Show("请输入运行周期数", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            string StratPos;
            if (RbHigh.Checked)
            {
                StratPos = "High";
            }
            else
            {
                StratPos = "Low";
            }
            string TempContrl;
            if (RBAir.Checked)
            {
                TempContrl = "Air";
            }
            else
            {
                TempContrl = "Sample";
            }
            string Ysfz;
            if (RBStandard.Checked)
            {
                Ysfz = "standard";
            }
            else if (RBLight.Checked)
            {
                Ysfz = "light";
            }
            else
            {
                Ysfz = "heavy";
            }
            decimal BeforTemp = this.NumBeforTemp.Value;
            decimal BeforTime = this.NumBeforTime.Value;
            if (BeforTime == 0)
            {
                MessageBox.Show("请输入预处理时长", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            decimal AfterTemp = this.NumAfterTemp.Value;
            decimal AfterTime = this.NumAfterTime.Value;
            if (AfterTime == 0)
            {
                MessageBox.Show("请输入尾处理时长", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //取当前时间并格式化
            DateTime time = DateTime.Now;
            string format = "yyyy-MM-dd HH:mm:ss";

            if (this.action == "create")
            {
                string CreateDate = time.ToString(format);
                IdWorker idWorker = new IdWorker(1, 1);
                string ID = idWorker.NextId().ToString();
                MySqlParameter[] paras = new MySqlParameter[] {
                    new MySqlParameter("@ID", ID),
                    new MySqlParameter("@ParaName", ParaName),
                    new MySqlParameter("@Remarks", Remarks),
                    new MySqlParameter("@ProductModel", ProductModel),
                    new MySqlParameter("@BatchNumber", BatchNumber),
                    new MySqlParameter("@CheckInterval", CheckInterval.ToString()),
                    new MySqlParameter("@PowerStepConfig", PowerStepConfig.ToString()),
                    new MySqlParameter("@HighTemp", HighTemp.ToString()),
                    new MySqlParameter("@LowTemp", LowTemp.ToString()),
                    new MySqlParameter("@HighTime", HighTime.ToString()),
                    new MySqlParameter("@LowTime", LowTime.ToString()),
                    new MySqlParameter("@IsLineContrl", IsLineContrl.ToString()),
                    new MySqlParameter("@XieLv", XieLv.ToString()),
                    new MySqlParameter("@TestCycs", TestCycs.ToString()),
                    new MySqlParameter("@StratPos", StratPos.ToString()),
                    new MySqlParameter("@TempContrl", TempContrl.ToString()),
                    new MySqlParameter("@BeforTemp", BeforTemp.ToString()),
                    new MySqlParameter("@BeforTime", BeforTime.ToString()),
                    new MySqlParameter("@AfterTemp", AfterTemp.ToString()),
                    new MySqlParameter("@AfterTime", AfterTime.ToString()),
                    new MySqlParameter("@Ysfz", Ysfz.ToString()),
                    new MySqlParameter("@CreateBy", this.person),
                    new MySqlParameter("@CreateDate", CreateDate),
                    new MySqlParameter("@UpdateBy", null),
                    new MySqlParameter("@UpdateDate", null),
                    new MySqlParameter("@isStatic", isStatic.ToString()) };
                //插入记录                          
                DBHelper.ExecuteNonQuery(DBSql.INSERT_HTRB_TEST_PARAMETER, 1, paras);
                UIMessageDialog.ShowMessageDialog(this, "新建测试参数方案成功", UILocalize.InfoTitle, false, UIStyle.Colorful);
                refreshData();
                this.Close();
            }
            if (this.action == "modify")
            {
                if (ParaName != oldParaName)//新建副本
                {
                    string CreateDate = time.ToString(format);
                    IdWorker idWorker = new IdWorker(1, 1);
                    string ID = idWorker.NextId().ToString();
                    MySqlParameter[] paras = new MySqlParameter[] {
                    new MySqlParameter("@ID", ID),
                    new MySqlParameter("@ParaName", ParaName),
                    new MySqlParameter("@Remarks", Remarks),
                    new MySqlParameter("@ProductModel", ProductModel),
                    new MySqlParameter("@BatchNumber", BatchNumber),
                    new MySqlParameter("@CheckInterval", CheckInterval.ToString()),
                    new MySqlParameter("@PowerStepConfig", PowerStepConfig.ToString()),
                    new MySqlParameter("@HighTemp", HighTemp.ToString()),
                    new MySqlParameter("@LowTemp", LowTemp.ToString()),
                    new MySqlParameter("@HighTime", HighTime.ToString()),
                    new MySqlParameter("@LowTime", LowTime.ToString()),
                    new MySqlParameter("@IsLineContrl", IsLineContrl.ToString()),
                    new MySqlParameter("@XieLv", XieLv.ToString()),
                    new MySqlParameter("@TestCycs", TestCycs.ToString()),
                    new MySqlParameter("@StratPos", StratPos.ToString()),
                    new MySqlParameter("@TempContrl", TempContrl.ToString()),
                    new MySqlParameter("@BeforTemp", BeforTemp.ToString()),
                    new MySqlParameter("@BeforTime", BeforTime.ToString()),
                    new MySqlParameter("@AfterTemp", AfterTemp.ToString()),
                    new MySqlParameter("@AfterTime", AfterTime.ToString()),
                    new MySqlParameter("@Ysfz", Ysfz.ToString()),
                    new MySqlParameter("@CreateBy", this.person),
                    new MySqlParameter("@CreateDate", CreateDate),
                    new MySqlParameter("@UpdateBy", null),
                    new MySqlParameter("@UpdateDate", null),
                    new MySqlParameter("@isStatic", isStatic.ToString())  };
                    //插入记录                          
                    DBHelper.ExecuteNonQuery(DBSql.INSERT_HTRB_TEST_PARAMETER, 1, paras);
                    UIMessageDialog.ShowMessageDialog(this, "新建测试参数方案副本成功", UILocalize.InfoTitle, false, UIStyle.Colorful);
                }
                else//更新数据
                {
                    string UpdateDate = time.ToString(format);
                    MySqlParameter[] paras = new MySqlParameter[] {
                        new MySqlParameter("@ParaName", ParaName),
                        new MySqlParameter("@Remarks", Remarks),
                        new MySqlParameter("@ProductModel", ProductModel),
                        new MySqlParameter("@BatchNumber", BatchNumber),
                        new MySqlParameter("@CheckInterval", CheckInterval.ToString()),
                        new MySqlParameter("@PowerStepConfig", PowerStepConfig.ToString()),
                        new MySqlParameter("@HighTemp", HighTemp.ToString()),
                        new MySqlParameter("@LowTemp", LowTemp.ToString()),
                        new MySqlParameter("@HighTime", HighTime.ToString()),
                        new MySqlParameter("@LowTime", LowTime.ToString()),
                        new MySqlParameter("@IsLineContrl", IsLineContrl.ToString()),
                        new MySqlParameter("@XieLv", XieLv.ToString()),
                        new MySqlParameter("@TestCycs", TestCycs.ToString()),
                        new MySqlParameter("@StratPos", StratPos.ToString()),
                        new MySqlParameter("@TempContrl", TempContrl.ToString()),
                        new MySqlParameter("@BeforTemp", BeforTemp.ToString()),
                        new MySqlParameter("@BeforTime", BeforTime.ToString()),
                        new MySqlParameter("@AfterTemp", AfterTemp.ToString()),
                        new MySqlParameter("@AfterTime", AfterTime.ToString()),
                        new MySqlParameter("@Ysfz", Ysfz.ToString()),
                        new MySqlParameter("@UpdateBy", this.person),
                        new MySqlParameter("@UpdateDate", UpdateDate),
                    new MySqlParameter("@isStatic", isStatic.ToString()) ,
                        new MySqlParameter("@ID", this.data["ID"].ToString()) };
                    //修改记录                     
                    DBHelper.ExecuteNonQuery(DBSql.UPDATE_HTRB_TEST_PARAMETER, 1, paras);
                    UIMessageDialog.ShowMessageDialog(this, "修改测试参数方案成功", UILocalize.InfoTitle, false, UIStyle.Colorful);
                }
                refreshData();
                this.Close();
            }

        }
        private void ConfigDetailForm_Load(object sender, EventArgs e)
        {
            if (this.action != "create")
            {
                this.textBoxParaName.Text = data["ParaName"].ToString();
                this.textBoxRemarks.Text = data["Remarks"].ToString();
                this.textBoxProductModel.Text = data["ProductModel"].ToString();
                this.textBoxBatchNumber.Text = data["BatchNumber"].ToString();
                this.NudCheckInterval.Value = int.Parse(data["CheckInterval"].ToString());

                string PowerGroupStepConfig = data["PowerStepConfig"].ToString();
                string[] PowerStepConfig = PowerGroupStepConfig.Split('-');
                for (int i = 0; i < PowerStepConfig.Length; i++)
                {
                    string[] temp = PowerStepConfig[i].Split(';');
                    PowerStepDataGrid.Rows.Add();
                    int rowIndex = PowerStepDataGrid.RowCount - 1;

                    //电源编号
                    ((DataGridViewComboBoxCell)PowerStepDataGrid.Rows[rowIndex].Cells[0]).Items.Add("DPS1");
                    ((DataGridViewComboBoxCell)PowerStepDataGrid.Rows[rowIndex].Cells[0]).Items.Add("DPS2");
                    ((DataGridViewComboBoxCell)PowerStepDataGrid.Rows[rowIndex].Cells[0]).Items.Add("DPS3");
                    ((DataGridViewComboBoxCell)PowerStepDataGrid.Rows[rowIndex].Cells[0]).Items.Add("DPS4");
                    ((DataGridViewComboBoxCell)PowerStepDataGrid.Rows[rowIndex].Cells[0]).Value = temp[1];
                    //起始电压
                    PowerStepDataGrid.Rows[rowIndex].Cells[1].Value = temp[2];
                    //目标电压
                    PowerStepDataGrid.Rows[rowIndex].Cells[2].Value = temp[3];
                    //步长电压
                    PowerStepDataGrid.Rows[rowIndex].Cells[3].Value = temp[4];
                    //动作间隔
                    PowerStepDataGrid.Rows[rowIndex].Cells[4].Value = temp[5];
                    //延迟时间
                    PowerStepDataGrid.Rows[rowIndex].Cells[5].Value = temp[6];
                }

                this.NumTestCycs.Value = int.Parse(data["TestCycs"].ToString());
                this.LowTemperatureValue.Value = int.Parse(data["LowTemp"].ToString());
                this.HighTemperatureValue.Value = int.Parse(data["HighTemp"].ToString());
                this.NumHighTime.Value = int.Parse(data["HighTime"].ToString());
                this.NumLowTime.Value = int.Parse(data["LowTime"].ToString());

                this.NumBeforTemp.Value = int.Parse(data["BeforTemp"].ToString());
                this.NumBeforTime.Value = int.Parse(data["BeforTime"].ToString());
                this.NumAfterTemp.Value = int.Parse(data["AfterTemp"].ToString());
                this.NumAfterTime.Value = int.Parse(data["AfterTime"].ToString());
                this.NumXieLv.Value = int.Parse(data["XieLv"].ToString());

                string IsLineContrl = data["IsLineContrl"].ToString();
                if (IsLineContrl == "Y")
                {
                    CBIsLineContrl.Checked = true;
                }
                else
                {
                    CBIsLineContrl.Checked = false;
                }

                if (data["StratPos"].ToString() == "High")
                {
                    RbHigh.Checked = true;
                }
                else
                {
                    RbLow.Checked = true;
                }

                if (data["TempContrl"].ToString() == "Air")
                {
                    RBAir.Checked = true;
                }
                else
                {
                    RBSample.Checked = true;
                }

                if (data["Ysfz"].ToString() == "standard")
                {
                    RBStandard.Checked = true;
                }
                else if (data["Ysfz"].ToString() == "light")
                {
                    RBLight.Checked = true;
                }
                else
                {
                    RBHeavy.Checked = true;
                }

                bool isStatic;
                bool.TryParse(data["isStatic"].ToString(), out isStatic);
                if (isStatic)
                {
                    uiRadioButton2.Checked = true;
                }
                else
                {

                    uiRadioButton1.Checked = true;
                }
            }

            if (this.action == "modify")
            {
                oldParaName = data["ParaName"].ToString();
            }

            if (this.action == "read")
            {
                this.textBoxParaName.ReadOnly = true;
                this.textBoxRemarks.ReadOnly = true;
                this.textBoxProductModel.ReadOnly = true;
                this.textBoxBatchNumber.ReadOnly = true;
                this.NudCheckInterval.ReadOnly = true;
                PowerStepDataGrid.ReadOnly = true;

                this.NumTestCycs.ReadOnly = true;
                this.LowTemperatureValue.ReadOnly = true;
                this.HighTemperatureValue.ReadOnly = true;
                this.NumHighTime.ReadOnly = true;
                this.NumLowTime.ReadOnly = true;

                this.NumBeforTemp.ReadOnly = true;
                this.NumBeforTime.ReadOnly = true;
                this.NumAfterTemp.ReadOnly = true;
                this.NumAfterTime.ReadOnly = true;
                this.NumXieLv.ReadOnly = true;

                CBIsLineContrl.ReadOnly = true;

                RbHigh.Enabled = false;
                RbLow.Enabled = false;

                RBAir.Enabled = false;
                RBSample.Enabled = false;

                RBStandard.Enabled = false;
                RBLight.Enabled = false;
                RBHeavy.Enabled = false;

                btnSave.Visible = false;
                AddButton.Visible = false;
                DeleButton.Visible = false;

                uiRadioButton1.ReadOnly = true;
                uiRadioButton2.ReadOnly = true;
            }
        }


        private void AddButton_Click(object sender, EventArgs e)
        {
            if (PowerStepDataGrid.RowCount >= 20)
            {
                UIMessageDialog.ShowMessageDialog(this, "最多只能配置20个步骤", UILocalize.InfoTitle, false, UIStyle.Colorful);
            }

            int rowIndex;
            if (PowerStepDataGrid.RowCount == 0)
            {
                this.PowerStepDataGrid.Rows.Add();
            }
            else
            {
                rowIndex = PowerStepDataGrid.RowCount - 1;

                //电源编号
                if (((DataGridViewComboBoxCell)PowerStepDataGrid.Rows[rowIndex].Cells[0]).Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：电源未选择！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string PowerNo = ((DataGridViewComboBoxCell)PowerStepDataGrid.Rows[rowIndex].Cells[0]).Value.ToString();
                if (string.IsNullOrEmpty(PowerNo))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：电源未选择！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //起始电压
                if (PowerStepDataGrid.Rows[rowIndex].Cells[1].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：起始电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string StartVolt = PowerStepDataGrid.Rows[rowIndex].Cells[1].Value.ToString();
                if (string.IsNullOrEmpty(StartVolt))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：起始电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DStrartVolt = double.Parse(StartVolt);
                    if (DStrartVolt < 0)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：起始电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：起始电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //目标电压
                if (PowerStepDataGrid.Rows[rowIndex].Cells[2].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：目标电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string EndVolt = PowerStepDataGrid.Rows[rowIndex].Cells[2].Value.ToString();
                if (string.IsNullOrEmpty(EndVolt))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：目标电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DEndVolt = double.Parse(EndVolt);
                    if (DEndVolt < 0)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：目标电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：目标电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //步长电压
                if (PowerStepDataGrid.Rows[rowIndex].Cells[3].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：步长电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string StepVolt = PowerStepDataGrid.Rows[rowIndex].Cells[3].Value.ToString();
                if (string.IsNullOrEmpty(StepVolt))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：步长电压未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DStepVolt = double.Parse(StepVolt);
                    if (DStepVolt < 0)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：步长电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：步长电压输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //动作间隔
                if (PowerStepDataGrid.Rows[rowIndex].Cells[4].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：动作间隔未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string IntervalTime = PowerStepDataGrid.Rows[rowIndex].Cells[4].Value.ToString();
                if (string.IsNullOrEmpty(IntervalTime))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：动作间隔未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DIntervalTime = double.Parse(IntervalTime);
                    if (DIntervalTime < 5 || DIntervalTime > 3600)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：动作间隔输入错误，必须大于等于5且小于等于3600！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：动作间隔输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                //延迟时间
                if (PowerStepDataGrid.Rows[rowIndex].Cells[5].Value == null)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：延迟时间未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;

                }
                string DelayTime = PowerStepDataGrid.Rows[rowIndex].Cells[5].Value.ToString();
                if (string.IsNullOrEmpty(DelayTime))
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：延迟时间未输入！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }
                try
                {
                    double DDelayTime = double.Parse(DelayTime);
                    if (DDelayTime < 0 || DDelayTime > 3600)
                    {
                        UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：延迟时间输入错误，必须大于等于0且小于等于3600！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                        return;
                    }
                }
                catch (Exception ex)
                {
                    UIMessageDialog.ShowMessageDialog(this, "步骤" + (rowIndex + 1) + "：延迟时间输入错误！", UILocalize.ErrorTitle, false, UIStyle.Colorful);
                    return;
                }

                this.PowerStepDataGrid.Rows.Add();
            }

            rowIndex = PowerStepDataGrid.RowCount - 1;

            ((DataGridViewComboBoxCell)this.PowerStepDataGrid.Rows[rowIndex].Cells[0]).Items.Add("DPS1");
            ((DataGridViewComboBoxCell)this.PowerStepDataGrid.Rows[rowIndex].Cells[0]).Items.Add("DPS2");
            ((DataGridViewComboBoxCell)this.PowerStepDataGrid.Rows[rowIndex].Cells[0]).Items.Add("DPS3");
            ((DataGridViewComboBoxCell)this.PowerStepDataGrid.Rows[rowIndex].Cells[0]).Items.Add("DPS4");
        }

        private void DeleButton_Click(object sender, EventArgs e)
        {
            if (this.PowerStepDataGrid.RowCount <= 0)
            {
                return;
            }
            if (this.PowerStepDataGrid.CurrentRow == null)
            {
                UIMessageDialog.ShowMessageDialog(this, "请先选择要删除的步骤", UILocalize.InfoTitle, false, UIStyle.Colorful);
                return;
            }
            this.PowerStepDataGrid.Rows.RemoveAt(this.PowerStepDataGrid.CurrentRow.Index);
        }

        private void uiRadioButton1_CheckedChanged(object sender, EventArgs e)
        {

            if (uiRadioButton1.Checked)
            {
                uiRadioButton2.Checked = false;
            }
        }

        private void uiRadioButton2_CheckedChanged(object sender, EventArgs e)
        {

            if (uiRadioButton2.Checked)
            {
                uiRadioButton1.Checked = false;
            }
        }
    }
}
